package com.food.eat.util;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.util.List;
import java.util.Map;

public class FormSubmission {
    public static void main(String[] args) {



        // 设置浏览器驱动路径（需替换为实际路径）
        System.setProperty("webdriver.chrome.driver", "C:\\drivers\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");   //C:\Users\12795\AppData\Local\Google\Chrome\Application
        WebDriver driver = new ChromeDriver();

        driver.get("http://uc-auth.ywzt.res.hagy.yc/auth/login?clientId=yth-portal&redirectUrl=http%3A%2F%2Fuc-auth.ywzt.res.hagy.yc%2Fauth%2Fq%2Fsso%2Fjwt%2FgetIdToken%2Fv1%2Fyth-portal%3Fenterpriseld%3Didaas%26target_url%3D%26target_url%3Dhttp%253A%252F%252Fyth.res.hagy.yc%252F%2523%252Fhome"); // 替换为实际页面地址
        WebDriverWait wait = new WebDriverWait(driver, 10); // 显式等待

        try {
            // 读取Excel数据
            List<Map<String, String>> materialList = ExcelParser.readExcel("C:\\Users\\12795\\Desktop\\用户信息.xlsx");

            for (Map<String, String> material : materialList) {
                // 填充物料编码
                WebElement codeInput = wait.until(ExpectedConditions.visibilityOfElementLocated(
                        By.id("materialCode"))); // 替换为物料编码输入框的实际ID
                codeInput.clear();
                codeInput.sendKeys(material.get("账号"));

                // 填充物料名称
                WebElement nameInput = driver.findElement(By.id("materialName")); // 替换实际ID
                nameInput.clear();
                nameInput.sendKeys(material.get("密码"));

                // 填充供应商名称
                WebElement supplierNameInput = driver.findElement(By.id("supplierName"));
                supplierNameInput.clear();
                supplierNameInput.sendKeys(material.get("供应商名称"));

                // 填充供应商地址
                WebElement addressInput = driver.findElement(By.id("supplierAddress"));
                addressInput.clear();
                addressInput.sendKeys(material.get("供应商地址"));

                // 提交表单（替换为实际提交按钮的定位方式）  //*[@id="appMain"]/div/div/div[2]/div[2]/div[2]/div/div/button
                WebElement submitBtn = driver.findElement(By.xpath("//button[text()='登录']"));
                submitBtn.click();

                // 验证提交结果（示例：等待成功提示出现）
                wait.until(ExpectedConditions.visibilityOfElementLocated(
                        By.className("success-toast")));
                System.out.println("提交成功：" + material.get("物料编码"));
            }
        } catch (Exception e) {
            System.out.println("提交失败：" + e.getMessage());
        } finally {
            driver.quit(); // 关闭浏览器
        }
    }
}